package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.BatchChangeBookingOrderPayEndTimeReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BatchChangeBookingOrderPayEndTimeCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceEngageOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.DataUtil;

public class Ts_BatchChangeBookingOrderPayEndTime {
	
	private static BatchChangeBookingOrderPayEndTimeReq batchChangeBookingOrderPayEndTimeReq;
	private static BatchChangeBookingOrderPayEndTimeCall batchChangeBookingOrderPayEndTimeCall;

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改预订订单支付截止时间");
	}

	@Before
	public void caseUp() {

		batchChangeBookingOrderPayEndTimeReq = new BatchChangeBookingOrderPayEndTimeReq();
		batchChangeBookingOrderPayEndTimeCall = new BatchChangeBookingOrderPayEndTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchChangeBookingOrderPayEndTimeCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify() {
		
		List<Long> orderList = batchChangeBookingOrderPayEndTimeReq.getOrderIds();
		
		for (int i = 0; i < orderList.size(); i ++) {
			
			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderList.get(i));
			
			DataUtil.verifyDateEquals(
					batchChangeBookingOrderPayEndTimeReq.getNewPayEndTime(),
					orderPo.getPaymentEndTime(), "验证支付截止时间");
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_001() {
		Logger.start(true, "下一个预订订单，修改支付截止时间延迟25小时");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusHours(25).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_002() {
		Logger.start(true, "下一个预订订单，修改支付截止时间提前5天");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().minusDays(5).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_003() {
		Logger.start(true, "多个预订订单，修改支付截止时间提前5天");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder2Order(false);

			long orderId1 = placeOrder.getOrderList().get(0).getOrderId();
			long orderId2 = placeOrder.getOrderList().get(1).getOrderId();

			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId1, orderId2));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().minusDays(5).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_004() {
		Logger.start(true, "下一个已经支付定金的预订订单，修改支付截止时间延迟2天");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long userId = placeOrder.getUserId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal earnestAmount = placeOrder.getOrderList().get(0)
					.getOrderPo().getEarnestAmount();

			TradingCallService.payEngageOrder(userId, earnestAmount, tradingId);
			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusDays(2).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_005() {
		Logger.start(true, "创建一个支付全额的预订订单，修改支付截止时间延迟3天，修改失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long userId = placeOrder.getUserId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder.getTradingId();
			BigDecimal earnestAmount = placeOrder.getOrderList().get(0)
					.getOrderPo().getEarnestAmount();

			TradingCallService.payEngageOrder(userId, earnestAmount, tradingId);
			TradingSyncService.getSyncResult(orderId);

			TradingCallService.payFullPaidOrder(userId, orderId);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");
			
			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);
			
			Date oldPaymentEndTime = orderPo.getPaymentEndTime();
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusDays(3).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			orderShardingWapper.clearCache();
			
			DataUtil.verifyDateEquals(
					oldPaymentEndTime,
					orderShardingWapper.selectOrderbyOrderId(orderId).getPaymentEndTime(), "验证支付截止时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_006() {
		Logger.start(true, "创建一个普通订单，修改支付截止时间延迟3天，修改失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusDays(3).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			orderShardingWapper.clearCache();
			
			DataUtil.verifyDateEquals(
					null,
					orderShardingWapper.selectOrderbyOrderId(orderId).getPaymentEndTime(), "验证支付截止时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_007() {
		Logger.start(true, "创建一个普通订单和一个预订订单，修改支付截止时间延迟3天，修改失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder1 = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			TradingServiceResp placeOrder2 = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long orderId1 = placeOrder1.getOrderList().get(0).getOrderId();
			long orderId2 = placeOrder2.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId1, orderId2));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusDays(3).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			orderShardingWapper.clearCache();
			
			DataUtil.verifyDateEquals(
					null,
					orderShardingWapper.selectOrderbyOrderId(orderId1).getPaymentEndTime(), "验证支付截止时间");
			DataUtil.verifyDateEquals(
					batchChangeBookingOrderPayEndTimeReq.getNewPayEndTime(),
					orderShardingWapper.selectOrderbyOrderId(orderId2).getPaymentEndTime(), "验证支付截止时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_008() {
		Logger.start(true, "创建一个普通订单和一个预订订单，修改支付截止时间延迟3天，修改失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder1 = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			TradingServiceResp placeOrder2 = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long orderId1 = placeOrder1.getOrderList().get(0).getOrderId();
			long orderId2 = placeOrder2.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId1, orderId2));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusDays(3).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			orderShardingWapper.clearCache();
			
			DataUtil.verifyDateEquals(
					null,
					orderShardingWapper.selectOrderbyOrderId(orderId1).getPaymentEndTime(), "验证支付截止时间");
			DataUtil.verifyDateEquals(
					batchChangeBookingOrderPayEndTimeReq.getNewPayEndTime(),
					orderShardingWapper.selectOrderbyOrderId(orderId2).getPaymentEndTime(), "验证支付截止时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchChangeBookingOrderPayEndTime_009() {
		Logger.start(true, "创建一个预订订单和一个已全额支付预订订单，修改支付截止时间延迟3天，修改失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder1 = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);
			TradingServiceResp placeOrder2 = TradingPlaceEngageOrderCallService
					.placeEngageOrder(false);

			long userId = placeOrder2.getUserId();
			long orderId2 = placeOrder2.getOrderList().get(0).getOrderId();
			long tradingId = placeOrder2.getTradingId();
			BigDecimal earnestAmount = placeOrder2.getOrderList().get(0)
					.getOrderPo().getEarnestAmount();

			TradingCallService.payEngageOrder(userId, earnestAmount, tradingId);
			TradingSyncService.getSyncResult(orderId2);

			TradingCallService.payFullPaidOrder(userId, orderId2);
			Thread.sleep(5000);

			long orderId1 = placeOrder1.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			
			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId2);
			
			Date oldPaymentEndTime = orderPo.getPaymentEndTime();
			
			// 准备参数
			batchChangeBookingOrderPayEndTimeReq.setOrderIds(Arrays.asList(orderId1, orderId2));
			batchChangeBookingOrderPayEndTimeReq.setNewPayEndTime(DateTime.now().plusDays(3).toDate());
			
			batchChangeBookingOrderPayEndTimeCall.setData(batchChangeBookingOrderPayEndTimeReq);
			batchChangeBookingOrderPayEndTimeCall.callService();
			
			Logger.verifyEquals(true, batchChangeBookingOrderPayEndTimeCall.getSuccess(), "验证success");
			
			orderShardingWapper.clearCache();
			
			DataUtil.verifyDateEquals(
					oldPaymentEndTime,
					orderShardingWapper.selectOrderbyOrderId(orderId2).getPaymentEndTime(), "验证支付截止时间");
			DataUtil.verifyDateEquals(
					batchChangeBookingOrderPayEndTimeReq.getNewPayEndTime(),
					orderShardingWapper.selectOrderbyOrderId(orderId1).getPaymentEndTime(), "验证支付截止时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
